// import { onMounted, onUnmounted, ref } from 'vue';
// import { throttle } from 'underscore';

// export default function useScroll() {
//   const isReachBottom = ref(false)
//   const clientHeight = ref(0)
//   const scrollTop = ref(0)
//   const scrollHeight = ref(0)
//     //监听windows滚动
//   const scrollList = throttle(() => {
//     clientHeight.value = document.documentElement.clientHeight
//     scrollTop.value = document.documentElement.scrollTop
//     scrollHeight.value = document.documentElement.scrollHeight
//     // console.log(clientHeight, scrollTop, scrollHeight)
//     if(clientHeight.value + scrollTop.value >= scrollHeight.value - 10) {
//       // homeStore.fetchHouseList()
//       // if(reachBottomCallback) reachBottomCallback() 方法一 传入函数
//       isReachBottom.value = true
//     }
//   }, 100)
//   window.addEventListener("scroll", scrollList)

//   onMounted(() => window.addEventListener("scroll", scrollList))
//   onUnmounted(() => window.addEventListener("scroll", scrollList))

//   return { isReachBottom, clientHeight, scrollTop, scrollHeight}
// }

//-----------------------------------------------------------------------------------------------

import { onMounted, onUnmounted, ref } from 'vue';
import { throttle } from 'underscore'

// console.log(throttle)


// export default function useScroll(reachBottomCB) {
//   const scrollListenerHandler = () => {
//     const clientHeight = document.documentElement.clientHeight
//     const scrollTop = document.documentElement.scrollTop
//     const scrollHeight = document.documentElement.scrollHeight
//     console.log("-------")
//     if (clientHeight + scrollTop >= scrollHeight) {
//       console.log("滚动到底部了")
//       if (reachBottomCB) reachBottomCB()
//     }
//   }
  
//   onMounted(() => {
//     window.addEventListener("scroll", scrollListenerHandler)
//   })
  
//   onUnmounted(() => {
//     window.removeEventListener("scroll", scrollListenerHandler)
//   })
// }

export default function useScroll() {
  const isReachBottom = ref(false)

  const clientHeight = ref(0)
  const scrollTop = ref(0)
  const scrollHeight = ref(0)

  // 防抖/节流
  const scrollListenerHandler = throttle(() => {
    clientHeight.value = document.documentElement.clientHeight
    scrollTop.value = document.documentElement.scrollTop
    scrollHeight.value = document.documentElement.scrollHeight
    if (clientHeight.value + scrollTop.value >= scrollHeight.value) {
      // console.log("滚动到底部了")
      isReachBottom.value = true
    }
  }, 100)
  
  onMounted(() => {
    window.addEventListener("scroll", scrollListenerHandler)
  })
  
  onUnmounted(() => {
    window.removeEventListener("scroll", scrollListenerHandler)
  })

  return { isReachBottom, clientHeight, scrollTop, scrollHeight }
}
